﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Model;
using DAL.BaseInterface;
using IDAL;
using System.Data;
using System.Data.SqlClient;
using ConfigHelper.Common;

namespace DAL
{
    public class Demodal : BaseDAL<DemoModel>, IDemodal
    {
        // 存放表名
        public override string TableName
        {
            get { return "DemoModel"; }
        }
        // 主键
        public override Dictionary<string, bool> PKname
        {
            get
            {
                Dictionary<string, bool> dic = new Dictionary<string, bool>();
                dic.Add("id", true);
                return dic;
            }
        }

        #region Demo列表页
        public List<DemoModel> GetDemoList(params SqlParameter[] param)
        {
            //调用common 中拼好的Sql语句
            string Sql = Common.GetList<DemoModel>(TableName, param);

            //调用DBHelper 返回DataTable类型
            DataTable dt = DBhelper.GetDataSet(Sql);

            List<DemoModel> admin = new List<DemoModel>();
            foreach (DataRow item in dt.Rows)
            {
                Type t1 = typeof(DemoModel);
                DemoModel t2 = (DemoModel)t1.Assembly.CreateInstance(t1.FullName);
                foreach (var i in t1.GetProperties())
                {
                    i.SetValue(t2, item[i.Name]);
                }
                admin.Add(t2);
            }
            return admin;
        }
        #endregion


        #region Demo执行添加
        public bool AddDemo(DemoModel d)
        {
            //调用common 中拼好的Sql语句
            string Sql = Common.AddSQL<DemoModel>(d, TableName, PKname);

            return DBhelper.GetExecuteNonQuery(Sql);
        }
        #endregion


        #region Demo执行删除
        public bool DelDemo(DemoModel d)
        {
            //调用common 中拼好的Sql语句
            string Sql = Common.DeleteSQL<DemoModel>(d, TableName, PKname);

            return DBhelper.GetExecuteNonQuery(Sql);
        }
        #endregion


        #region Demo执行修改
        public bool UpdateDemo(DemoModel d)
        {
            //调用common 中拼好的Sql语句
            string Sql = Common.UpdateSQL<DemoModel>(d, TableName, PKname);

            return DBhelper.GetExecuteNonQuery(Sql);
        }
        #endregion


    }

}
